package com.zhousb.deviceManager.utils;


import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Component
@Slf4j
public class OSSUtil {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint = "";
    @Value("${aliyun.oss.region}")
    private String region = "";
    @Value("${aliyun.oss.access-key-id}")
    private String accessKeyId = "";
    @Value("${aliyun.oss.access-key-secret}")
    private String accessKeySecret = "";
    @Value("${aliyun.oss.bucket-name}")
    private String bucketName = "";
    OSS ossClient;
    @PostConstruct
    private void initOssClient() {
        log.info("初始化oss:{}",accessKeyId);
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();
    }

    @PreDestroy
    void close(){
        ossClient.shutdown();
    }

    public String upload(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String finalFileName;
            String fileName = file.getOriginalFilename();
            if(!StringUtils.hasText(fileName)){
               return null;
            }
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            finalFileName =  UUID.randomUUID().toString().concat(suffix);
            ossClient.putObject(bucketName, finalFileName, inputStream);
            return "https://".concat(bucketName).concat(".").concat(endpoint).concat("/").concat(finalFileName);
        } catch (
                OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (
                IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }


}
